Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting counts from raw samples #2686

Merged
merged 71 commits into from
Jun 25, 2022
Merged

Conversation

theodotk
Copy link
Contributor

@theodotk theodotk commented Jun 11, 2022

Context:

Fixing #2562

Description of the Change:

Adding the possibility to group samples into counts providing counts=True flag in qml.sample function. This required creation of a new measurement type Counts in measurements.py.

The implementation is based on editing the sample function in _qubit_device.py, though it was possible to base it on estimate_probability function (or to create something new that seems superfluous to me). Choice of editing the sample function seemed more natural for this task, but please tell me if that's not the best approach.

Examples

From the issue:

dev = qml.device('default.qubit', wires=3, shots=10)

@qml.qnode(dev)
def my_circ():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0,1])
    qml.PauliX(wires=2) 

    return qml.sample(counts=True)```

```>>> my_counts = my_circ()
>>> print(my_counts)
{'001': 6, '111': 4}

It works also for observables:


@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0, 1])
    return qml.sample(qml.PauliZ(0), counts=True), qml.sample(qml.PauliZ(1), counts=True)```

```>>>result = circuit()
>>>print(result)
[tensor({-1: 472, 1: 528}, dtype=object, requires_grad=True)
 tensor({-1: 472, 1: 528}, dtype=object, requires_grad=True)]

Benefits:

Better visibility of the sampling result.

Possible Drawbacks:

Shouldn't be, as the change scope was minimal.

Related GitHub Issues:
2562

@codecov
Copy link

codecov bot commented Jun 11, 2022

Codecov Report

Merging #2686 (0fb203b) into master (38ca383) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2686   +/-   ##
=======================================
  Coverage   99.61%   99.61%           
=======================================
  Files         255      255           
  Lines       20884    20913   +29     
=======================================
+ Hits        20804    20833   +29     
  Misses         80       80           
Impacted Files Coverage Δ
pennylane/_qubit_device.py 99.18% <100.00%> (+0.04%) ⬆️
pennylane/interfaces/autograd.py 100.00% <100.00%> (ø)
pennylane/measurements.py 99.62% <100.00%> (+<0.01%) ⬆️
pennylane/qnode.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 38ca383...0fb203b. Read the comment docs.

@theodotk theodotk marked this pull request as draft June 13, 2022 22:19
@theodotk theodotk marked this pull request as ready for review June 13, 2022 22:19
@theodotk
Copy link
Contributor Author

Hey @josh146, can you please re-launch the tests?
I'm also not sure how to interpret code coverage test results and if I can improve it.
Thanks

@theodotk
Copy link
Contributor Author

Thanks @josh146 ! Sorry, I didn't think of checking the tests as well. Can you please relaunch?
Thanks again!

@theodotk
Copy link
Contributor Author

Thanks, @josh146 !
Do you know whom should I ask for a review? The author of the original issue, or there is someone responsible for the measurement/qubit device part of the code?

@theodotk theodotk changed the title Supporting counts from raw samples [WIP] Supporting counts from raw samples Jun 14, 2022
@theodotk theodotk changed the title [WIP] Supporting counts from raw samples Supporting counts from raw samples Jun 14, 2022
@josh146 josh146 requested a review from Jaybsoni June 15, 2022 01:45
@josh146
Copy link
Member

josh146 commented Jun 15, 2022

Thanks @theodotk! Someone from the team will have a look shortly :)

@theodotk
Copy link
Contributor Author

Hey @Jaybsoni @antalszava could you please review the PR?
It's my test assignment for the job interview hence the time limit, and I mention both of you because you are in the initial issue discussion.
Thanks!

@Jaybsoni
Copy link
Contributor

Hey @theodotk,

Thanks for the contribution, I will take a look at it today! I think @antalszava will take a closer look at it at some point once he is back in the office as he is overlooking the interview process. Thanks for you patience.

Cheers,

@antalszava
Copy link
Contributor

antalszava commented Jun 23, 2022

Hi @theodotk, it seems that changes made in the Autograd interface would be required for other interfaces too because they too cannot interpret dictionaries (e.g., trying to set interface="jax" fails on a jnp.array conversion).

If it doesn't seem to come around, no worries, I could help here tomorrow or latest next week. 👍

@theodotk
Copy link
Contributor Author

theodotk commented Jun 24, 2022

Hi @antalszava ! I fixed the problem with jax, and both tensorflow and torch return expected result with the new version.

The problem arose because jax does not handle non-numeric types such as string. And in case of state vector sampling, keys of the counts dictionary are strings (because str is hashable and because converting binary to decimal instead is a less user-friendly option). But the dictionary itself seems to be handled okay.
So the code became slightly uglier 😢 but it works.

Just in case, I chose interface by setting qml.device("default.qubit.jax") (replacing "jax" by "tf" or "torch" for the others). Tell me if it isn't the right way to test.

@antalszava
Copy link
Contributor

Hi @theodotk, that's awesome! 🙂

It would be nice then to have a simple example for each interface, e.g.,

import pennylane as qml

dev = qml.device('default.qubit', wires=3, shots=10)

@qml.qnode(dev, inteface="jax")
def circuit():
    return qml.sample(counts=True)

circuit()

and then this PR should be looking great! 🌟

@theodotk
Copy link
Contributor Author

@antalszava you mean in the docs?

@antalszava
Copy link
Contributor

@theodotk apologies! I meant such examples as test cases so that we can be sure things are and stay okay. 👍

@theodotk
Copy link
Contributor Author

@antalszava done!

Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @theodotk! 🥇 Great one with getting each detail right here, thanks for addressing the comments! 🙂

Made a couple of small changes to test docstrings and names, but will merge this soon.

@antalszava antalszava merged commit fbda189 into PennyLaneAI:master Jun 25, 2022
@theodotk
Copy link
Contributor Author

Thanks @antalszava , it is pleasant to see such a diligent review process!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants